//存放主要交互逻辑的 js 代码
/*
    因为 js 代码很容易写乱掉，
    所以使用类似 java 代码的 包.类.方法 的方式书写
 */

var ManagerSystem = {

    //用来保存所需要访问的 URL
    URL: {
        //登录页面
        login: function () {
            return "login";
        },
        //详情页
        loginSuccess: function (eusername) {
            return eusername + "/detail";
        },
        //注销
        logout: function () {
          return window.document.location.href+"/logout";
        },
        //个人信息页面
        roleDetail: function(){
            return window.document.location.href+"/personal";
        },
        //所有部门页面
        allDepartment:function(){
            return window.document.location.href+"/allDepartment";
        },
        //部门详情页面
        department:function(){
            return window.document.location.href+"/department";
        },
        //员工信息页面
        employee:function(){
            return window.document.location.href+"/employeeList";
        },
        //查询员工用户名和编号是否存在
        queryExist:function(){
            return window.document.location.href+"/query/Exist";
        },
        //添加部门页面
        addDepartment:function(){
            return window.document.location.href+"/add/Department";
        },
        //添加员工页面
        addEmployee:function () {
            return window.document.location.href+"/add/Employee";
        },
        //保存部门页面
        saveDepartment:function () {
            return window.document.location.href+"/save/Department";
        },
        //保存员工页面
        saveEmployee:function () {
            return window.document.location.href+"/save/Employee";
        },
        //删除部门
        deleteDepartment:function () {
            return window.document.location.href+"/delete/Department";
        },
        //删除员工
        deleteEmployee:function () {
            return window.document.location.href+"/delete/Employee";
        },
        //修改部门信息
        editDepartment:function () {
            return window.document.location.href+"/edit/Department";
        },
        //修改员工信息
        editEmployee:function () {
            return window.document.location.href+"/edit/Employee";
        },
        //修改密码
        eidtPassword:function(){
            return window.document.location.href+"/edit/Password";
        },
        //保存修改后的部门信息
        updateDepartment:function () {
            return window.document.location.href+"/update/Department";
        },
        //保存修改后的员工信息
        updateEmployee:function () {
            return window.document.location.href+"/update/Employee";
        },
        updatePassword:function(){
            return window.document.location.href+"/update/Password";
        }
    },

    //一些工具函数
    util: {
        //用来模拟表单提交一个 post 请求的函数
        postForm: function (URL, PARAMS) {
            var temp = document.createElement("form");
            temp.action = URL;
            temp.method = "post";
            temp.style.display = "none";
            for (var x in PARAMS) {
                var opt = document.createElement("textarea");
                opt.name = x;
                opt.value = PARAMS[x];
                temp.appendChild(opt);
            }
            document.body.appendChild(temp);
            temp.submit();
            return temp;
        },

        //用来获取项目的根目录
        rootPath:function(){
            //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
            var curWwwPath = window.document.location.href;
            //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
            var pathName = window.document.location.pathname;
            var pos = curWwwPath.indexOf(pathName);
            //获取主机地址，如： http://localhost:8083
            var localhostPath = curWwwPath.substring(0, pos);
            //获取带"/"的项目名，如：/uimcardprj
            var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
            return (localhostPath + projectName + '/');
        },
    },

    //用来处理和登录页面相关的业务
    loginPage: {
        login: function (params) {
            //接收从 index.jsp 通过 el 表达式传递过来的参数
            var eusername = params['eusername'];
            var epassword = params['epassword'];
            //将用户名保存到session中

            //向 /login 发起 ajax 请求
            $.post(ManagerSystem.URL.login(), {"eusername": eusername, "epassword": epassword}, function (result) {
                if (result && result['login']) {
                    ManagerSystem.util.postForm(ManagerSystem.URL.loginSuccess(eusername));
                } else {
                    $('#errorMessage').hide().html(result['result']).show(300);
                }
            })
        }
    },

    logoutPage : {
        logout: function () {
            console.log("saf ");
            window.location.href=ManagerSystem.URL.logout();
        }
    },

    //用来处理和详细信息页面相关的业务
    detailPage: {

        //详细信息页面初始化
        init: function (params) {
            var eid = params['eid'];
            var rid = params['rid'];
            var did = params['did'];
            var currentPage = 1;
            if (rid == 1) {
                $('#ul').html('<li>\n' +
                    '                    <a href="javascript:ManagerSystem.detailPage.personalDetail('+eid+')">个人信息</a>\n' +
                    '                </li>\n' +
                    '                <li>\n' +
                    '                    <a href="javascript:void(0)" onclick="javascript:ManagerSystem.detailPage.allDepartmentDetail('+currentPage+')">部门管理</a>\n' +
                    '                </li>\n' +
                    '                <li>\n' +
                    '                    <a href="javascript:void(0)" onclick="javascript:ManagerSystem.detailPage.employeeList('+JSON.stringify({rid:rid,did:did,currentPage:currentPage}).replace(/"/g, '&quot;')+')">员工管理</a>\n' +
                    '                </li>');
            } else {
                $('#ul').html('<li>\n' +
                    '                    <a href="javascript:ManagerSystem.detailPage.personalDetail('+eid+')">个人信息</a>\n' +
                    '                </li>\n' +
                    '                <li>\n' +
                    '                    <a href="javascript:void(0)" onclick="javascript:ManagerSystem.detailPage.departmentDetail(' + did + ')">部门详情</a>\n' +
                    '                </li>\n' +
                    '                <li>\n' +
                    '                    <a href="javascript:void(0)" onclick="javascript:ManagerSystem.detailPage.employeeList('+JSON.stringify({rid:rid,did:did,currentPage:currentPage}).replace(/"/g, '&quot;')+')">部门员工</a>\n' +
                    '                </li>');
            }
        },

        //个人信息页跳转
        personalDetail:function(eid){
            $.post(ManagerSystem.URL.roleDetail(),{"eid":eid},function(result){
                console.log(result);
                if(result == null){
                    console.log("个人信息页错误");
                }else{
                    $('#right').html(result);
                }
            });
        },

        //部门信息页跳转
        departmentDetail:function(did){
            $.post(ManagerSystem.URL.department(),{"did":did},function(result){
                if(result == null){
                    console.log("部门详情页错误");
                }else {
                    $('#right').html(result);
                }
            });
        },

        //所有部门页跳转
        allDepartmentDetail:function(currentPage){
            $.post(ManagerSystem.URL.allDepartment(),{"currentPage":currentPage},function(result){
                if(result == null){
                    console.log("所有部门页错误");
                }else {
                    $('#right').html(result);
                }
            });
        },

        //员工列表页跳转
        employeeList:function(params){
            var rid = params['rid']
            var did = params['did'];
            var currentPage = params['currentPage'];
            console.log("asd");
            $.post(ManagerSystem.URL.employee(),{"rid":rid,"did":did,"currentPage":currentPage},function(result){
                if(result == null){
                    console.log("员工列表页错误");
                }else {
                    $('#right').html(result);
                }
            })
        },
    },

    //用来处理添加记录的业务
    addPage:{
        addDepartment:function() {
            $.post(ManagerSystem.URL.addDepartment(), {}, function (result) {
                if(result == null){
                    console.log("添加部门请求错误");
                }else {
                    $('#right').html(result);
                }
            })
        },
        addEmployee:function(params){
            var rid = params['rid'];
            var did = params['did'];
            $.post(ManagerSystem.URL.addEmployee(),{"rid":rid,"did":did},function(result){
                if(result == null){
                    console.log("添加员工请求错误");
                }else{
                    $('#right').html(result);
                }
            })
        },
    },

    //用来处理保存记录的业务
    savePage:{
        saveDepartment:function (params) {
            var dname = params['dname'];
            var ddesc = params['ddesc'];
            $.post(ManagerSystem.URL.saveDepartment(),{"dname":dname,"ddesc":ddesc},function (result) {
                console.log(result);
                if(result == null){
                    console.log("保存部门请求错误");
                }else{
                    $('#right').html(result);
                }
            })
        },
        saveEmployee:function (params) {
            var ename = params['ename'];
            var egender = params['egender'];
            var edepartment = params['edepartment'];
            var erole = params['erole'];
            var enumber = params['enumber'];
            var eusername = params['eusername'];
            var ebirthday = params['ebirthday'];
            var ejoindate = params['ejoindate'];
            var rid = params['rid'];
            var did = params['did'];

            var isExist = null;

            //查询员工编号和用户名是否已存在
            $.post(ManagerSystem.URL.queryExist(),{"enumber":enumber,"eusername":eusername},function(result){
                isExist = result;
                if(isExist == "-2"){
                    alert("员工用户名重复！");
                }
                if(isExist == "-1"){
                    alert("员工编号重复！");
                }
            });

            if(ename == null || ename.trim() == ""){
                alert("员工姓名不能为空！");
            }else if(edepartment == 0){
                alert("未选择员工部门！");
            }else if(erole == 0){
                alert("未选择员工职位！");
            }else if(enumber == null || enumber.trim() == "") {
                alert("未填写员工编号！");
            }else if(eusername == null || eusername.trim() == ""){
                alert("未填写员工用户名！");
            }else if(ebirthday == null || ebirthday.trim() == ""){
                alert("未填写员工生日！");
            }else if(ejoindate == null || ejoindate.trim() == ""){
                alert("未填写入职日期！");
            }else{
                $.post(ManagerSystem.URL.saveEmployee(),
                       {"ename":ename,
                        "egender":egender,
                        "edepartment.did":edepartment,
                        "erole.rid":erole,
                        "enumber":enumber,
                        "eusername":eusername,
                        "ebirthday":ebirthday,
                        "ejoindate":ejoindate,
                           "rid":rid,
                           "did":did
                       },
                       function(result){
                            if(result == null){
                                console.log("请求保存员工失败！");
                            }else{
                                $('#right').html(result);
                            }
                       });
            }
        }
    },

    //用来处理编辑记录的业务
    editPage:{
        editDepartment:function (params) {
            var did = params['did'];
            var currentPage = params['currentPage'];
            $.post(ManagerSystem.URL.editDepartment(),{"did":did,"currentPage":currentPage},function (result) {
                if(result == null){
                    console.log("编辑部门请求错误")
                }else {
                    $('#right').html(result);
                }
            })
        },
        editEmployee:function (params) {
            var eid = params['eid'];
            var rid = params['rid'];
            var did = params['did'];
            $.post(ManagerSystem.URL.editEmployee(),{"eid":eid,"rid":rid,"did":did},function(result){
                if(result == null){
                    console.log("请求编辑员工页面错误！")
                }else{
                    $('#right').html(result);
                }
            })
        },
        editPassword:function(params){
            var eid = params['eid'];
            var epassword = params['epassword'];
            var ename = params['ename'];
            $.post(ManagerSystem.URL.eidtPassword(),{"eid":eid , "epassword":epassword , "ename":ename},function(result){
                $('#right').html(result);
            })
        }
    },

    //用来保存修改后记录的业务
    updatePage:{
        updateDepartment:function (params) {
            var currentPage = params['currentPage'];
            var did = params['did'];
            var dname = params['dname'];
            var ddesc = params['ddesc'];
            $.post(ManagerSystem.URL.updateDepartment(),{"did":did,"dname":dname,"ddesc":ddesc},function(result){
                if(result == null){
                    console.log("保存修改记录错误")
                }else{
                    $('#right').html(result);
                }
            })
        },
        updateEmployee:function (params) {
            var eid = params['eid'];
            var ename = params['ename'];
            var egender = params['egender'];
            var edepartment = params['edepartment'];
            var erole = params['erole'];
            var enumber = params['enumber'];
            var eusername = params['eusername'];
            var ebirthday = params['ebirthday'];
            var ejoindate = params['ejoindate'];
            var rid = params['rid'];
            var did = params['did'];
            $.post(ManagerSystem.URL.updateEmployee(),
                        {"eid":eid,
                            "ename":ename,
                            "egender":egender,
                            "edepartment.did":edepartment,
                            "erole.rid":erole,
                            "enumber":enumber,
                            "eusername":eusername,
                            "ebirthday":ebirthday,
                            "ejoindate":ejoindate,
                            "rid":rid,
                            "did":did},
                        function(result){
                            if(result == null){
                                console.log("修改员工信息失败！")
                            }else{
                                $('#right').html(result);
                            }
                        })
        },
        updatePassword:function(params){
            var eid = params['eid'];
            var epassword = params['epassword'];
            if(epassword.trim() == "" || epassword == null){
                alert("密码不能为空！")
            }else {
                $.post(ManagerSystem.URL.updatePassword() , {"eid":eid,"epassword":epassword} , function(result){
                    $('#right').html(result);
                })
            }
        }
    },

    //用来处理删除记录的业务
    deletePage:{
        deleteDepartment:function(params){
            var did = params['did'];
            var currentPage = params['currentPage'];
            $.post(ManagerSystem.URL.deleteDepartment(),{"did":did},function (result) {
                if(result == null){
                    console.log("删除部门请求失败");
                }else{
                    $('#right').html(result);
                }
            })
        },
        deleteEmployee:function(params){
            var eid = params['eid'];
            var rid = params['rid'];
            var did = params['did'];
            $.post(ManagerSystem.URL.deleteEmployee(),{"eid":eid,"rid":rid,"did":did},function (result) {
                if(result == null){
                    console.log("删除员工请求失败！")
                }else{
                    $('#right').html(result);
                }
            })
        }
    }
}